<template>
	<view class="">
		<view class="mycontent">
			<input type='text' style="margin: 20rpx;" v-model="ssid" placeholder="请输入wifi的SSID" disabled=true><br>
			<input type='text' style="margin: 20rpx;" v-model="psw" placeholder="请输入wifi的密码"><br>
			<button type="primary" style="margin: 20rpx;" @click="doSmartConfig">一键配网</button>
		</view>
		<view class="button-sp-area">
			<button type="primary" plain="true" @click="showRichAlert()">RichAlert测试</button>
			<button type="primary" plain="true" @click="getWifiInfo()">getWifiInfo</button>
			<button type="primary" plain="true" @click="smartConfig()">smartConfig</button>
			<button type="primary" plain="true" @click="cancel()">cancel smartConfig</button>
		</view>
	</view>
	
</template>

<script>
	// 获取 module
	var smartConfigModule = uni.requireNativePlugin("SmartConfigModule")
	// var backPopupModule = uni.requireNativePlugin("BackPopupModule")
	const modal = uni.requireNativePlugin('modal');
	
	
	export default {
		data() {
			return {
				ssid:"abcccc",
				psw:"66666666"
			}
		},
		onLoad() {
			let that = this;
			//监听android原生端调用的前端方法：wifiChange，用于修改ssid（当wifi切换时自动会触发）
			plus.globalEvent.addEventListener('wifiChange', function(data){
				that.ssid = data.ssid;//当wifi切换时，修改ssid文本框
			});
			
			this.initSmartConfig();
			// backPopupModule.startTelephoneService();
			
		},
		methods: {
			
			initSmartConfig(){
				smartConfigModule.initSmartConfig((ret) =>{
					if(ret.code!=200){
						modal.toast({
							message: "SmartConfig初始化失败",
							duration: 3
						});
					}else{
						modal.toast({
							message: "SmartConfig初始化成功",
							duration: 3
						});
					}
				});
			},
			doSmartConfig(){
				uni.showLoading({
					'title':'开始配网……'
				});
				smartConfigModule.doSmartConfig({
					'psw':this.psw,
					'deviceCount':1
				},(ret)=>{
					uni.hideLoading();
					console.log(ret);
					if(ret.code==200){
						modal.toast({
							message: ret.ip,
							duration: 3
						});
					}else{
						modal.toast({
							message: ret.msg,
							duration: 3
						});
					}
				});
			}
			
		}
	}
</script>
<style>
	page {
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		box-sizing: border-box;
		background-color: #fff;
		min-height: 100vh;
		height: auto;
	
	}
	
	view {
		font-size: 14px;
		line-height: inherit;
		margin-left: 20rpx;
		margin-right: 20rpx;
	}
</style>